package com.zjt.demo;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.iceteck.silicompressorr.SiliCompressor;
import com.zjt.pictureselector.PictureSelectorActivity;
import com.zjt.pictureselector.model.MediaBean;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public static final int SELECT_REQUEST_CODE = 100;
    private TextView tvPic;
    private TextView tvVideoCompress;
    private ArrayList<MediaBean> selectMediaList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        tvVideoCompress = (TextView) this.findViewById(R.id.tv_video_compress);
        tvVideoCompress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                videoCompress();
            }
        });
        tvPic = (TextView) this.findViewById(R.id.tv_pic_selector);
        tvPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, PictureSelectorActivity.class);
                intent.putParcelableArrayListExtra(PictureSelectorActivity.SELECT_LIST, selectMediaList);
                startActivityForResult(intent, SELECT_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            if (selectMediaList != null && !selectMediaList.isEmpty()){
                selectMediaList.clear();
            }
            selectMediaList.addAll((ArrayList)data.getParcelableArrayListExtra(PictureSelectorActivity.SELECT_LIST));
            showData();
        }
    }

    private void showData(){
        for (MediaBean mediaBean: selectMediaList) {
            Log.d("media=", mediaBean.getPath() + " " + mediaBean.getType());
        }
    }

    private void videoCompress(){
        if (selectMediaList == null && selectMediaList.isEmpty()) return;
        for (MediaBean mediaBean: selectMediaList) {
            if (mediaBean.getType() == MediaBean.Type.Video) {
                File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + "/media/videos");
                if (f.mkdirs() || f.isDirectory()) {
                    Log.d("media=", mediaBean.getPath());
                    new VideoCompressAsyncTask(this).execute(mediaBean.getPath(), f.getPath());
                }
            }
        }
    }

    class VideoCompressAsyncTask extends AsyncTask<String, String, String> {

        Context mContext;

        public VideoCompressAsyncTask(Context context){
            mContext = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... paths) {
            String filePath = null;
            try {
                filePath = SiliCompressor.with(mContext).compressVideo(paths[0], paths[1]);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            return  filePath;
        }

        @Override
        protected void onPostExecute(String compressedFilePath) {
            super.onPostExecute(compressedFilePath);
            File imageFile = new File(compressedFilePath);
            float length = imageFile.length() / 1024f; // Size in KB
            String value;
            if(length >= 1024)
                value = length/1024f+" MB";
            else
                value = length+" KB";
            Log.d("media=", value);
        }
    }
}
